package com.example.android_pti_backup;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	Button b;
	EditText et, pass;
	TextView tv;
	HttpPost httppost;
	StringBuffer buffer;
	HttpResponse response;
	HttpClient httpclient;
	List<NameValuePair> nameValuePairs;
	ProgressDialog dialog = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		b = (Button) findViewById(R.id.buttonLogin);
		et = (EditText) findViewById(R.id.textUsername);
		pass = (EditText) findViewById(R.id.textPassword);
		tv = (TextView) findViewById(R.id.tv);
		
		b.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog = ProgressDialog.show(MainActivity.this, "",
						"Validasi Login", true);
				new Thread(new Runnable() {

					@Override
					public void run() {
						login();

					}
				}).start();

			}
		});

		// Button login = (Button) findViewById(R.id.buttonLogin);
		// login.setOnClickListener(new View.OnClickListener() {
		//
		// public void onClick(View v) {
		// // TODO Auto-generated method stub
		// EditText usernameInput = (EditText) findViewById(R.id.textUsername);
		// Intent i = new Intent(MainActivity.this, IsiPesanan.class);
		// i.putExtra("pesan", "Welcome " + usernameInput.getText());
		// startActivity(i);
		// }
		// });

	}

	public void login() {
		try {

			httpclient = new DefaultHttpClient();
			httppost = new HttpPost("http://10.0.2.2/login.php");
			nameValuePairs = new ArrayList<NameValuePair>(2);
			nameValuePairs.add(new BasicNameValuePair("username", et.getText()
					.toString().trim()));
			nameValuePairs.add(new BasicNameValuePair("password", pass
					.getText().toString().trim()));
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
		
			response = httpclient.execute(httppost);
			
			ResponseHandler<String> responseHandler = new BasicResponseHandler();
			final String response = httpclient.execute(httppost,
					responseHandler);
			System.out.println("Response : " + response);
			runOnUiThread(new Runnable() {
				public void run() {
					tv.setText("Response from PHP : " + response);
					dialog.dismiss();
				}
			});

			if (response.matches("KoneksiDB OK <br/>Login Sukses")) {
				runOnUiThread(new Runnable() {
					public void run() {
						Toast.makeText(MainActivity.this, "Login Sukses",
								Toast.LENGTH_SHORT).show();
					}
				});

				startActivity(new Intent(MainActivity.this, IsiPesanan.class));
			} else {
				showAlert();
			}

		} catch (Exception e) {
			dialog.dismiss();
			System.out.println("Exception : " + e.getMessage());
		}
	}

	public void showAlert() {
		MainActivity.this.runOnUiThread(new Runnable() {
			public void run() {
				AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
				builder.setTitle("Login Error");
				builder.setMessage("User Tidak Ditemukan").setCancelable(false).setPositiveButton("OK",
								new DialogInterface.OnClickListener() {
									
									@Override
									public void onClick(DialogInterface dialog, int id) {
										
									}
								});
				AlertDialog alert = builder.create();
				alert.show();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
